import { _decorator, Component, Label } from 'cc';
import { DataProxy } from '../../../frame/A-LIB/lib.b.data';
import GlobalDC from '../../../frame/common/data.global.center';
import Config from '../../manager/config.manager';
import GameDC from '../../manager/data.game.center';
import GameAssets from '../../manager/game.assets';
const { ccclass, property } = _decorator;

@ccclass('Statistics')
export class Statistics extends Component {

	protected onLoad(): void {
		this.renderLocalUI();
		this.listening();
	}

	private listening(){
		let [lbl_pass, lbl_challenge, lbl_average] = ['round', 'challenge', 'free'].map(name=>this.node.getChildByName('lbl-'+name+'-value').getComponent(Label));
		let lbl_title = this.node.getChildByName('lbl-head').getComponent(Label);

		let date = new Date();
		let day_str = '';
		DataProxy.follow(GameDC.Status, 'statistics_count', (o, v)=>lbl_pass.string = String(v));
		DataProxy.follow(GameDC.Status, 'statistics_round', (o, v)=>lbl_challenge.string = String(v));
		DataProxy.follow(GameDC.Status, 'statistics_free', (o, v)=>lbl_average.string = Config.symbolString(v));
		DataProxy.follow(GlobalDC.Clock, 'current_time', (o, v)=>{

			date.setTime(v);
			let dstr = [date.getFullYear(), date.getMonth()+1, date.getDate()].map(v=>v.toString().padStart(2, '0')).join('.');
			if(day_str == dstr){
				return void 0;
			}

			day_str = dstr;
			lbl_title.string = GameAssets.getLangText(23, dstr);
		});
	}

	private renderLocalUI(){
		(<const>[
			['round', 24],
			['challenge', 25],
			['free', 26]
		]).forEach(([name, idx])=>this.node.getChildByName('lbl-'+name+'-head').getComponent(Label).string = GameAssets.getLangText(idx));
	}
}

